package com.coda.controller;

import com.coda.annotation.AutoFill;
import com.coda.common.PageResult;
import com.coda.common.Result;
import com.coda.dto.AssignRoleDto;
import com.coda.dto.PageDto;
import com.coda.entity.Auth;
import com.coda.entity.Role;
import com.coda.entity.User;
import com.coda.mapper.UserMapper;
import com.coda.service.AuthService;
import com.coda.service.RoleService;
import com.coda.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.jaxb.SpringDataJaxb;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/warehouse/user")
@Api(tags = "用户相关接口")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private AuthService authService;


    /**
     * 加载权限菜单
     */
    @GetMapping("/auth-list")
    @ApiOperation("加载权限菜单")
    public Result getAutoMenu() {
        List<Auth> authList = authService.getauthList();
        return Result.ok(authList);
    }

    /**
     * 分页查询用户列表
     */
    @GetMapping("/user-list")
    @ApiOperation("分页查询用户列表")
    public Result getUserList(PageDto pageDto, User user) {
        PageResult userList = userService.getUserList(pageDto, user);
        return Result.ok(userList);
    }

    /**
     * 添加用户
     */
    @PostMapping("/addUser")
    @ApiOperation("添加用户")
    public Result addUser(@RequestBody User user) {
        userService.addUser(user);
        return Result.ok("添加成功");
    }

    /**
     * 启用和禁用用户
     */
    @PutMapping("/updateState")
    @ApiOperation("启用和禁用用户")
    public Result updateState(@RequestBody User user) {
        userService.updateState(user);
        return Result.ok("修改成功");
    }

    /**
     * 根据id查询用户角色
     */
    @GetMapping("/user-role-list/{id}")
    @ApiOperation("根据id查询用户角色")
    public Result getRoleList(@PathVariable Long id) {
        List<Role> roleList = roleService.getRoleListByUserId(id);
        return Result.ok(roleList);
    }

    /**
     * 根据id添加用户角色
     */
    @PutMapping("/assignRole")
    @ApiOperation("根据id添加用户角色")
    public Result addUserRole(@RequestBody AssignRoleDto assignRoleDto) {
        roleService.addUserRole(assignRoleDto);
        return Result.ok("分配成功");
    }

    /**
     * 删除单个用户
     */
    @DeleteMapping("/deleteUser/{id}")
    @ApiOperation("根据id删除单个用户“")
    public Result deleteUserById(@PathVariable Long id) {
        userService.deleteUserById(id);
        return Result.ok("删除成功");
    }

    /**
     * 批量删除用户
     */
    @DeleteMapping("/deleteUserList")
    @ApiOperation("批量删除用户")
    public Result deleteUserList(@RequestBody List<Long> ids) {
        userService.deleteUserList(ids);
        return Result.ok("删除成功");
    }

    /**
     * 根据id修改用户
     */
    @PutMapping("/updateUser")
    @ApiOperation("根据id修改用户")
    public Result updateUser(@RequestBody User user) {
        userService.updateUser(user);
        return Result.ok("修改成功");
    }

    /**
     * 根据id重置密码
     */
    @PutMapping("/updatePwd/{id}")
    @ApiOperation("根据id重置密码")
    public Result updatePwd(@PathVariable Long id) {
        userService.updatePwd(id);
        return Result.ok("重置成功");
    }

}
